package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.BloodInformation;
import server.business.pojo.OverallBlood;
import server.business.pojo.ReturnBloodStation;
import server.business.pojo.ReturnedBloodIn;
import server.business.service.BloodInformationService;
import server.business.service.OverallBloodService;
import server.business.service.ReturnBloodStationService;
import server.business.service.ReturnedBloodInService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.results.zy.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * 血液退回血站模块
 *
 * @Author 赵越
 * @Time 2022-08-03
 */
@RestController
@RequestMapping("/bloodcanclestation")
public class BloodCancleStationController {

    /**
     * 装配血液退回血站服务
     */
    @Autowired
    private ReturnBloodStationService returnBloodStationService;

    /**
     * 引入血液信息相关功能
     */
    @Autowired
    private OverallBloodService overallBloodService;

    /**
     * 引入退血入库相关功能
     */
    @Autowired
    private ReturnedBloodInService returnedBloodInService;

    /**
     * 引入血液基础信息相关功能
     */
    @Autowired
    private BloodInformationService bloodInformationService;

    /**
     * 是否允许退血模块
     *
     * @param number        患者病历号
     * @param transfusion   患者输血申请单号
     * @param isAllowedBack 是否允许退血（1表示允许，0表示不允许）
     * @return 返回是否允许退血结果
     */
    @RequestMapping(value = "/normal/isagree", method = RequestMethod.POST)
    public Result bloodCancleAgree(@RequestParam("number") Integer number, @RequestParam("transfusion") String transfusion, @RequestParam("isAllowedBack") Integer isAllowedBack) {
        overallBloodService.updateIsAllowedBackByNumberAndTransfusion(number, transfusion, isAllowedBack);
        return Result.success();
    }

    /**
     * 根据血站病历号和输血单号查询血站输血信息
     *
     * @return 返回血站所有输血信息
     */
    @RequestMapping(value = "/normal/find", method = RequestMethod.GET)
    public Result bloodCancleAgree(@RequestParam("number") Integer number, @RequestParam("transfusion") String transfusion) {
        OverallBlood all = overallBloodService.findByNumberAndTransfusion(number, transfusion);
        if (all == null) {
            return Result.error("600", "查询为空");
        } else {
            return Result.success(all);
        }
    }

    /**
     * 血站状态退血信息
     *
     * @param currentPage 当前页面
     * @param pageSize    当前页面大小
     * @return 返回血站审批通过的退血信息
     */
    @RequestMapping(value = "/normal/findbackinformation/{currentPage}/{pageSize}", method = RequestMethod.GET)
    public Result stationFindBackInformationByCondition(@PathVariable int currentPage, @PathVariable int pageSize, ReturnBloodStation returnBloodStation) {
        IPage<ReturnBloodStation> page = returnBloodStationService.getPageByState(currentPage, pageSize, returnBloodStation);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = returnBloodStationService.getPageByState((int) page.getPages(), pageSize, returnBloodStation);
        }
        if (page == null) {
            return Result.error("600", "查询为空");
        } else {
            return Result.success(page);
        }
    }

    /**
     * 血站退血入库模块
     *
     * @param returnedBloodIn 退血入库信息包装类
     * @return 返回血站退血入库结果
     */
    @RequestMapping(value = "/normal/in", method = RequestMethod.POST)
    public Result stationBloodBackIn(@RequestBody ReturnedBloodIn returnedBloodIn) {
        returnedBloodInService.returnBloodIn(returnedBloodIn);
        String number = returnedBloodIn.getBackInformationNumber();
        returnedBloodInService.setIsAllowBackByNumber(number, 0);
        returnedBloodInService.deleteBackInformationByStationNumber(number);
        return Result.success();
    }

    /**
     * 血站在库血液信息查询模块
     *
     * @param currentPage 当前页面
     * @param pageSize    当前页面大小
     * @return 返回所有血站在库的血液信息
     */
    @RequestMapping(value = "/normal/instationinformation/{currentPage}/{pageSize}", method = RequestMethod.GET)
    public Result findBloodInformation(@PathVariable int currentPage, @PathVariable int pageSize, BloodInformation bloodInformation) {
        IPage<BloodInformation> page = bloodInformationService.getPageBi(currentPage, pageSize, bloodInformation);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = bloodInformationService.getPageBi((int) page.getPages(), pageSize, bloodInformation);
        }
        if (page == null) {
            return Result.error("600", "查询为空");
        } else {
            return Result.success(page);
        }
    }

    /**
     * 血液退回血站申请模块
     *
     * @param returnBloodStation 血站退血信息包装类
     * @return 返回血液退回血站申请结果
     */
    @RequestMapping(value = "/normal/application", method = RequestMethod.POST)
    public Result bloodCancleStationApplication(@RequestBody ReturnBloodStation returnBloodStation) {
        returnBloodStationService.insertBackInformation(returnBloodStation);
        String number = returnBloodStation.getBackInformationNumber();
        returnBloodStationService.setConditionByNumber(number, "23");
        String bloodNumber = returnBloodStation.getBloodId();
        bloodInformationService.setConditionByBloodNumber(bloodNumber, "23");
        return Result.success();
    }

    /**
     * 血液退回血站修改模块
     *
     * @param returnBloodStation 血站退血信息包装类
     * @return 返回血液退回血站修改结果
     */
    @RequestMapping(value = "/normal/modify", method = RequestMethod.PUT)
    public Result bloodCancleStationModify(@RequestBody ReturnBloodStation returnBloodStation) {
        String number = returnBloodStation.getBackInformationNumber();
        ReturnBloodStation updateReturnBloodStation = new ReturnBloodStation();
        updateReturnBloodStation.setGmtModified(returnBloodStation.getGmtModified());
        updateReturnBloodStation.setBackBloodReason(returnBloodStation.getBackBloodReason());
        updateReturnBloodStation.setApplicantName(returnBloodStation.getApplicantName());
        updateReturnBloodStation.setApplicantNumber(returnBloodStation.getApplicantNumber());
        updateReturnBloodStation.setApplyTime(returnBloodStation.getApplyTime());
        updateReturnBloodStation.setState(returnBloodStation.getState());
        updateReturnBloodStation.setAuditor(returnBloodStation.getAuditor());
        updateReturnBloodStation.setAuditorNumber(returnBloodStation.getAuditorNumber());
        updateReturnBloodStation.setAuditTime(returnBloodStation.getAuditTime());
        returnBloodStationService.updateReturnStationByNumber(number, updateReturnBloodStation);
        return Result.success();
    }

    /**
     * 血液退回血站删除
     *
     * @param returnBloodStation 血站退血信息包装类
     * @return 返回血液退回血站删除结果
     */
    @RequestMapping(value = "/normal/delete", method = RequestMethod.DELETE)
    public Result bloodCancleStationDelete(@RequestBody ReturnBloodStation returnBloodStation) {
        String number = returnBloodStation.getBackInformationNumber();
        String bloodId = returnBloodStation.getBloodId();
        returnBloodStationService.deleteReturnStationByNumber(number);
        bloodInformationService.setConditionByBloodNumber(bloodId, "2");
        return Result.success();
    }

    /**
     * 血液退回血站审批
     *
     * @param returnBloodStation 血站退血信息包装类
     * @return 血液退回血站申请审批结果
     */
    @RequestMapping(value = "/review/approval", method = RequestMethod.POST)
    public Result bloodCancleStationApproval(@RequestBody ReturnBloodStation returnBloodStation) {
        String number = returnBloodStation.getBackInformationNumber();
        String auditor = returnBloodStation.getAuditor();
        String auditorNumber = returnBloodStation.getAuditorNumber();
        Date auditTime = returnBloodStation.getAuditTime();
        returnBloodStationService.setAuditAndConditionByNumber(number, auditor, auditorNumber, auditTime, "24");
        return Result.success();
    }

    /**
     * 血液退回血站驳回
     *
     * @param returnBloodStation 血站退血信息包装类
     * @return 血液退回血站申请驳回结果
     */
    @RequestMapping(value = "/review/reject", method = RequestMethod.POST)
    public Result bloodCancleStationReject(@RequestBody ReturnBloodStation returnBloodStation) {
        String number = returnBloodStation.getBackInformationNumber();
        String auditor = returnBloodStation.getAuditor();
        String auditorNumber = returnBloodStation.getAuditorNumber();
        Date auditTime = returnBloodStation.getAuditTime();
        returnBloodStationService.setAuditAndConditionByNumber(number, auditor, auditorNumber, auditTime, "25");
        return Result.success();
    }

    /**
     * 血站退血信息模块
     *
     * @param currentPage        当前页面
     * @param pageSize           当前页面大小
     * @param returnBloodStation 血站退血信息
     * @return 返回血站退血申请信息
     */
    @RequestMapping(value = "/normal/information/{currentPage}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public Result bloodCancleStationInformation(@PathVariable int currentPage, @PathVariable int pageSize, ReturnBloodStation returnBloodStation) {
        IPage<ReturnBloodStation> page = returnBloodStationService.getPage(currentPage, pageSize, returnBloodStation);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = returnBloodStationService.getPage((int) page.getPages(), pageSize, returnBloodStation);
        }
        if (page == null) {
            return Result.error("600", "查询为空");
        } else {
            return Result.success(page);
        }
    }

    /**
     * 生成血站退血单号
     *
     * @param id 编号
     * @return 返回生成的血站退血单号
     */
    @RequestMapping(value = "/normal/getbacknumber", method = RequestMethod.GET)
    public Result getBackNumber(@RequestParam("id") Integer id) {
        GenerateNumberBySnowFlake backNumber = new GenerateNumberBySnowFlake(id, "No.");
        String res = backNumber.genNumberByflag();
        return Result.success(res);
    }

    /**
     * 血站退血申请信息批量打印
     *
     * @param ids 编号列表
     */
    @GetMapping(value = "normal/batchdownloadpba")
    @ResponseBody
    public void batchDownloaPba(HttpServletResponse response, @RequestParam ArrayList<Long> ids) throws IOException {
        bloodInformationService.batchDownloadPba(response, ids);
    }

    /**
     * 血站退血审核信息批量打印
     *
     * @param ids 编号列表
     */
    @GetMapping(value = "normal/batchdownloadsba")
    @ResponseBody
    public void batchDownloaSba(HttpServletResponse response, @RequestParam ArrayList<Long> ids) throws IOException {
        returnBloodStationService.batchDownload(response, ids);
    }

    /**
     * 批量删除
     *
     * @param ids 待删除数据的编号
     * @return 返回删除结果
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public Result batchDelete(@RequestBody ArrayList<Long> ids) {
        boolean success = returnBloodStationService.deleteBatch(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @param ids 待删除数据的编号
     * @return 返回删除结果
     */
    @RequestMapping("/normal/batchDeleteSb")
    @ResponseBody
    public Result batchDeleteSb(@RequestBody ArrayList<Long> ids) {
        boolean success = bloodInformationService.deleteBatch(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }

}
